package com.oocl.o2o.merchant.controller.rest;

import static com.oocl.o2o.merchant.util.ImageUtil.getAbsolutePath;
import static com.oocl.o2o.merchant.util.ImageUtil.getSuffix;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.oocl.o2o.merchant.entity.Merchant;
import com.oocl.o2o.merchant.service.MerchantManager;

@RestController
@RequestMapping("/sec/rest/merchant")
public class MerchantController {
	@Autowired
	private MerchantManager merchantManager;

	@GetMapping
	public Merchant getMerchant(@SessionAttribute String merchantId) {
		Merchant merchant = merchantManager.getMerchant(merchantId);
		return merchant;
	}

	@GetMapping("/{username}")
	public boolean isExit(@PathVariable("username") String username) {
		Merchant merchant = merchantManager.getMerchantByUsername(username);
		if (merchant == null)
			return false;
		else
			return true;
	}

	@PostMapping
	public boolean addMerchant(String username, String password, String address, MultipartFile portrait,
			MultipartFile idCard) {
		File portraitFile = null;
		File idCardFile = null;
		try {
			// packing
			String portraitPath = "/portrait/" + username + getSuffix(portrait.getOriginalFilename());
			String idCardPath = "/idCard/" + username + getSuffix(idCard.getOriginalFilename());
			Merchant merchant = new Merchant(null, username, password, idCardPath, portraitPath, address);
			// transfer to file
			byte[] portraitByte = portrait.getBytes();
			byte[] idCardByte = idCard.getBytes();
			portraitFile = new File(getAbsolutePath(portraitPath));
			idCardFile = new File(getAbsolutePath(idCardPath));
			portrait.transferTo(portraitFile);
			idCard.transferTo(idCardFile);
			merchant = merchantManager.register(merchant, portraitByte, idCardByte);
			if (merchant == null) {
				portraitFile.delete();
				idCardFile.delete();
				return false;
			}
			return true;
		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
			portraitFile.delete();
			idCardFile.delete();
			return false;
		}
	}

	@PostMapping("/update.action")
	public boolean updateMerchant(@SessionAttribute String merchantId, @SessionAttribute String username,
			String address, MultipartFile portrait, MultipartFile idCard) {
		File portraitFile = null;
		File idCardFile = null;
		try {
			String portraitPath = "/portrait/" + username + getSuffix(portrait.getOriginalFilename());
			String idCardPath = "/idCard/" + username + getSuffix(idCard.getOriginalFilename());
			byte[] portraitByte = portrait.getBytes();
			byte[] idCardByte = idCard.getBytes();
			portraitFile = new File(getAbsolutePath(portraitPath));
			idCardFile = new File(getAbsolutePath(idCardPath));
			portrait.transferTo(portraitFile);
			idCard.transferTo(idCardFile);
			Merchant merchant = merchantManager.reAudit(merchantId, address, idCardPath, portraitPath, idCardByte,
					portraitByte);
			if (merchant == null) {
				portraitFile.delete();
				idCardFile.delete();
				return false;
			}
			return true;

		} catch (IllegalStateException | IOException e) {
			e.printStackTrace();
			portraitFile.delete();
			idCardFile.delete();
			return false;
		}
	}
}
